package com.cskaoyan.controller.wxcontroller;

import com.cskaoyan.bean.bo.AuthLoginBo;
import com.cskaoyan.bean.bo.AuthRegisterBO;
import com.cskaoyan.bean.bo.AuthResetBO;
import com.cskaoyan.bean.bo.Mobile;
import com.cskaoyan.bean.bo.wx.LoginByWeixinBo;
import com.cskaoyan.bean.pojo.User;
import com.cskaoyan.bean.vo.AuthRegisterVo;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.RegisterUserInfoVO;
import com.cskaoyan.beanwx.vo.AuthLoginVo;
import com.cskaoyan.exception.UserLoginException;
import com.cskaoyan.realm.MallToken;
import com.cskaoyan.service.UserService;
import com.cskaoyan.utils.DateTransforUtil;
import com.cskaoyan.utils.SMSUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Date;

/**
 * @author: lds
 * @clsss: AuthController
 */
@RestController(value = "wxAuthController")
@RequestMapping("wx/auth")
public class AuthController {

    @Autowired
    UserService userService;
    /**
     * @author: lds
     * @title: authLogin
     * @description: 微信端登录
     * @param: [authLoginBo]
     * @return: com.cskaoyan.bean.vo.WxBaseRespVo
     */
    @RequestMapping("login")
    public BaseRespVo authLogin(@RequestBody AuthLoginBo authLoginBo) throws UserLoginException {
        Subject subject = SecurityUtils.getSubject();

        try {
            subject.login(new MallToken(authLoginBo.getUsername(), authLoginBo.getPassword(),"wx"));
        } catch (Exception e) {
          throw new UserLoginException(null,"用户名或密码不正确");
        }
        String sessionID = (String) subject.getSession().getId();
        long timeout = subject.getSession().getTimeout();
//        System.err.println(timeout);
        Date date = new Date(timeout);
        Date tokenExpire = DateTransforUtil.dateTransformed2(date);
        User user = (User) subject.getPrincipal();
        AuthLoginVo authLoginVo = new AuthLoginVo();
        AuthLoginVo.UserInfoBean userInfoBean = new AuthLoginVo.UserInfoBean();
        userInfoBean.setNickName(user.getNickname());
        userInfoBean.setAvatarUrl(user.getAvatar());
        authLoginVo.setUserInfo(userInfoBean);
        authLoginVo.setToken(sessionID);
        authLoginVo.setTokenExpire( tokenExpire);
        return BaseRespVo.ok(authLoginVo);
    }

    @RequestMapping("login_by_weixin")
    public BaseRespVo loginByWexin(@RequestBody LoginByWeixinBo loginByWeixinBo) throws UserLoginException {
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(new MallToken(loginByWeixinBo.getUserInfo().getNickName(), "000","wx"));
        } catch (Exception e) {
            throw new UserLoginException(null,"用户名或密码不正确");
        }
        String sessionID = (String) subject.getSession().getId();
        long timeout = subject.getSession().getTimeout();
//        System.err.println(timeout);
        Date date = new Date(timeout);
        Date tokenExpire = DateTransforUtil.dateTransformed2(date);
        User user = (User) subject.getPrincipal();
        AuthLoginVo authLoginVo = new AuthLoginVo();
        AuthLoginVo.UserInfoBean userInfoBean = new AuthLoginVo.UserInfoBean();
        userInfoBean.setNickName(user.getNickname());
        userInfoBean.setAvatarUrl(user.getAvatar());
        authLoginVo.setUserInfo(userInfoBean);
        authLoginVo.setToken(sessionID);
        authLoginVo.setTokenExpire( tokenExpire);
        return BaseRespVo.ok(authLoginVo);
    }


    /**
     * @author: lds
     * @title: authLogout
     * @description: 微信端登出
     * @param: []
     * @return: com.cskaoyan.bean.vo.WxBaseRespVo
     */
    @RequestMapping("logout")
    public BaseRespVo authLogout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return BaseRespVo.ok();
    }
    @RequestMapping("regCaptcha")
    public BaseRespVo authRegCaptcha(@RequestBody Mobile mobile){
        SMSUtil.verificationCode(mobile.getMobile());
        return BaseRespVo.ok();
    }
    @RequestMapping("register")
    public BaseRespVo authRegister(@RequestBody AuthRegisterBO authRegisterBO){
        if (!"65536".equals(authRegisterBO.getCode())){
            return BaseRespVo.fail4("验证码错误");
        }
        AuthRegisterVo authRegisterVo=userService.insertUser(authRegisterBO);
        return BaseRespVo.ok(authRegisterVo);
    }
    @RequestMapping("reset")
    public BaseRespVo authReset(@RequestBody AuthResetBO authResetBO) {
        if (!"65536".equals(authResetBO.getCode())){
            return BaseRespVo.fail4("验证码错误");
        }
        userService.updateUser(authResetBO);
        return BaseRespVo.ok();
    }
}
